<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<link rel="stylesheet" href="<{'static/css/bootstrap/bootstrap.min.css'|url}>" />
		<link rel="stylesheet" href="<{'static/css/bootstrap/bootstrap-responsive.min.css'|url}>" />
		<!--[if lt IE 6]> <link rel="stylesheet" href="<{'static/css/bootstrap/bootstrap-ie6.min.css'|url}>" /> <![endif]-->
		<{include file='html.tpl'}>
		<link rel="stylesheet" href="<{'static/css/unicorn.main.css'|url}>" />
		<link rel="stylesheet" href="<{'static/css/unicorn.grey.css'|url}>" class="skin-color" />

		<{include file="form.tpl"}>

		<script src="<{'static/js/bootstrap/bootstrap.min.js'|url}>"></script>
		<!--[if lt IE 6]> <script src="<{'static/js/bootstrap/bootstrap-ie.js'|url}>"></script> <![endif]-->

	</head>
	<body>
		<div id="header">
			<h1><a href="<{'admin'|url}>">Home</a></h1>	
		</div>
		<{include file="admin/nav.tpl"}>

		<{include file="admin/sidebar.tpl"}>

		<{include file="admin/style-switcher.tpl"}>
		
		<div id="content">
			<div id="content-header">
				<h1>編輯用戶組</h1>
				<{include file="admin/widget.tpl"}>
			</div>
			<div id="breadcrumb">
				<a href="<{'admin'|url}>" title="返回首頁" class="tip-bottom"><i class="icon-home"></i> 後臺</a>
				<a href="<{'admin/group'|url}>">用戶組管理</a>
				<a href="<{'admin/group/edit'|url}>" class="current">編輯用戶組</a>
			</div>
			<div class="container-fluid">
				<div class="row-fluid">
					<div class="span12">
						<div class="pull-right btn-group">
							<a class="btn btn-small btn-inverse" href="#new" data-backdrop="static" data-toggle="modal"><i class="icon icon-plus icon-white"></i> 添加用戶組</a>
							<a class="btn btn-small btn-success" href="<{'admin/group/fields_list'|url}>"><i class="icon icon-plus icon-white"></i> 修改權限字段</a>
							<a class="btn btn-small" href="javascript:" onclick="javascript:location.reload();"><i class="icon icon-refresh"></i> 刷新頁面</a>
						</div>
						<div class="clearfix"></div>
						<div class="widget-box">
							<div class="widget-title">
								<span class="icon">
									<i class="icon-align-justify"></i>									
								</span>
								<h5>用戶組</h5>
							</div>
							<div class="widget-content">
							<div class="alert alert-block alert-error">
							  <button type="button" class="close" data-dismiss="alert">&times;</button>
								<h4>警告!</h4>
								在操作本頁面之前，請確認您知道您在做什麼！
							</div>
							<form action="<{'admin/group/edit_auth_query'|url}>" method="POST" notheme="notheme">
								<table class="table table-striped table-bordered with-check" notheme="notheme">
									<thead>
										<tr>
											<th>#</th>
											<{foreach $_data.data as $key => $item}>
											<th><{$item.group_name|escape}></th>
											<{/foreach}>
										</tr>
										<tr>
											<td>GID</td>
											<{foreach $_data.data as $key => $item}>
											<td><{$item.gid|escape}></td>
											<{/foreach}>
										</tr>
										<tr>
											<td>操作</td>
											<{foreach $_data.data as $key => $item}>
											<td data-gid="<{$item.gid|escape}>" data-group_name="<{$item.group_name|escape}>" data-description="<{$item.description|escape}>">
												<a href="#update" name="update" data-toggle="modal" name="update" class="tip-top" data-original-title="修改" data-backdrop="static" ><i class="icon-pencil"></i></a>
												<{if !in_array($item.gid, array(0,1,99))}>
												<a href="#delete" name="delete" class="tip-top" data-original-title="刪除用戶組" data-backdrop="static" data-toggle="modal"><i class="icon-trash"></i></a>
												<{/if}>
											</td>
											<{/foreach}>
										</tr>
									</thead>
									<tbody>
									<{foreach $_data.fields as $key1 => $item1}>
										<tr name="group_line">
											<td><{$item1.text|escape}><br /><{$item1.auth_name|escape}></td>
											<{foreach $_data.data as $key => $item}>
											<{if $item1.type == 'boolean'}>
											<td><input type="checkbox" name="<{$item1.auth_name|escape}>[<{$item.gid|escape}>]" value="1" <{if $item[$item1.auth_name] > 0}>checked="checked"<{/if}> id="input-<{$item.gid|escape}>-<{$item1.id|escape}>" /></td>
											<{else}>
											<td><input type="text" name="<{$item1.auth_name|escape}>[<{$item.gid|escape}>]" value="<{$item[$item1.auth_name]|escape}>" style="width:40px;" id="input-<{$item.gid|escape}>-<{$item1.id|escape}>" /></td>
											<{/if}>
											<{/foreach}>
										</tr>
									<{/foreach}>
									</tbody>
									<tfoot>
										<tr>
											<td></td>
											<td colspan="<{$_data.data|@count}>"><button type="submit" class="btn btn-primary">提交修改</button></td>
										</tr>
									</tfoot>
								</table>
								</form>
							</div>
						</div>			
					</div>
				</div>
				
				<{include file="admin/copyright.tpl"}>
			</div>
		</div>
		<div id="new" class="modal hide fade">
			<div class="modal-header">
				<button data-dismiss="modal" class="close" type="button">×</button>
				<h3>增加用戶組</h3>
			</div>
			<form action="<{'admin/group/add_query'|url}>" method="POST">
			<div class="modal-body">
				<div class="widget-content nopadding">		
					<div class="control-group">
						<div class="controls">名稱：
							<input type="text" name="group_name" value="" placeholder="請輸入名稱..." /> 
						</div>
						<div class="controls">介紹：
							<textarea name="description" placeholder="選填"></textarea>
						</div>
					</div>
				</div>
			</div>
			<div class="modal-footer">
				<button type="submit" class="btn btn-primary" href="javascript:">修改</button>
				<a data-dismiss="modal" class="btn" href="#">取消</a>
			</div>
			</form>
		</div>
		<div id="update" class="modal hide fade">
			<div class="modal-header">
				<button data-dismiss="modal" class="close" type="button">×</button>
				<h3>修改用戶組 - <span id="update_group_name"></span></h3>
			</div>
			<form action="" id="update_form" confirm="您確認修改此用戶組嗎？" method="POST">
			<div class="modal-body">
				<div class="widget-content nopadding">		
					<div class="control-group">
						<div class="controls">名稱：
							<input type="text" name="group_name" value="" placeholder="請輸入名稱..." /> 
						</div>
						<div class="controls">介紹：
							<textarea name="description" placeholder="選填"></textarea>
						</div>
					</div>
				</div>
			</div>
			<div class="modal-footer">
				<button type="submit" class="btn btn-primary" href="javascript:">修改</button>
				<a data-dismiss="modal" class="btn" href="#">取消</a>
			</div>
			</form>
		</div>
		<div id="delete" class="modal hide fade">
			<div class="modal-header">
				<button data-dismiss="modal" class="close" type="button">×</button>
				<h3>刪除用戶組 - <span id="delete_group_name"></span></h3>
			</div>
			<form action="" id="delete_form" confirm="您確認刪除此用戶組嗎？此操作不可恢復" method="POST">
			<div class="modal-body">
				<div class="widget-content nopadding">		
					<div class="control-group">
						<div class="controls">將用戶轉移到：
							<select name="target_gid" notheme="notheme">
								<option value="0">請選擇用戶組</option>
								<{foreach $_data.data as $key => $item}>
								<{if !empty($item.gid)}><option value="<{$item.gid|escape}>"><{$item.group_name|escape}></option><{/if}>
								<{/foreach}>
							</select>
						</div>
						
					</div>
				</div>
			</div>
			<div class="modal-footer">
				<button type="submit" class="btn btn-primary" href="javascript:">修改</button>
				<a data-dismiss="modal" class="btn" href="#">取消</a>
			</div>
			</form>
		</div>
<{include file="admin/footer.tpl"}>
<script type="text/javascript">
(function($){
$().ready(function(){

	var group_fields = <{$_data.fields|@json_encode}>;
	$('input','[name="group_line"]').each(function(){ //配置data
		var $this = $(this);
		var data = $this.attr('id').split('-');
		var fields = group_fields[parseInt(data[2])];
		$this.data(fields);
		$this.data('gid',parseInt(data[1]));
	}).on('disabled', function(e, disabled, self){
		var $this = $(this);
		var gid = parseInt($this.data('gid'));
		var children = $this.data('children');
		if (!self) $this.prop('disabled', disabled);
		var checked = $this.is(':checkbox') ? $this.prop('checked') : true; //文本框例外
		if (!disabled && !checked) disabled = true; //如果父項並未選中,則任然保持disabled
		for (var i = 0; i < children.length; i++)
			$('#input-'+gid+'-'+children[i]).triggerHandler("disabled", [disabled, false]);
	}).filter(":checkbox").on('click', function(){
		var $this = $(this);
		$this.triggerHandler("disabled", [!this.checked, true]);
	}).each(function(){
		$(this).triggerHandler("click");
	});
	$('form,a[method]').query();

	$('a[name="update"]').on('click',function(){
		var $td = $(this).closest("td");
		var gid = parseInt($td.data('gid'));
		var $update_form = $('#update_form').attr('action','<{'admin/group/edit_query'|url}>?gid=' + gid);
		$('input[name="group_name"]',$update_form).val($td.data('group_name'));
		$('input[name="description"]',$update_form).val($td.data('description'));
		$('#update_group_name').text($td.data('group_name'));
	});
	$('a[name="delete"]').on('click',function(){
		var $td = $(this).closest("td");
		var gid = parseInt($td.data('gid'));
		var $delete_form = $('#delete_form').attr('action','<{'admin/group/delete_query'|url}>?gid=' + gid);
		var $select = $('select[name="target_gid"]', $delete_form)
		$('option', $select).prop('disabled',false).filter("[value='"+gid+"']").prop('disabled',true);
		$('#delete_group_name').text($td.data('group_name'));
	});
});
})(jQuery);
</script>
	</body>
</html>
